package rsj.proxy.bind;

import com.rlyy.aibase.utils.base.ThinkException;
import org.springframework.stereotype.Component;
import rsj.proxy.parse.ParamTypeEnum;
import rsj.proxy.parse.ParameterMapping;
import rsj.proxy.parse.SqlParse;
import rsj.proxy.parse.SqlSource;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * 参数是实体类数据类型-解析
 *
 * @author yuanqinglong
 * @since 2021/5/27 15:59
 */
@Component
public class EntityParamResolveProcessor implements ParamResolveProcessor {


    @Override
    public ParamTypeEnum getSupportType() {
        return ParamTypeEnum.ENTITY;
    }


    @Override
    public void doParamResolve(SqlSource sqlSource, ParameterMapping parameterMapping,
                                 Method method, Object[] args, List<Object> resolveParamList) throws Exception {
        // 获取这个占位符名称对应接口方法参数下标位置的实体类
        Object entity = args[parameterMapping.getMethodParamIndex()];
        Map<String, Method> getMethodMap = sqlSource.getGetMethodMap();
        // 获取此参数对应实体相同名称属性的get方法
        Method invokeMethod = getMethodMap.get(parameterMapping.getPlaceholderName());
        if (invokeMethod == null) {
            throw new ThinkException(SqlParse.generateMethodFullClassPath(method) + "获取不到 " + parameterMapping.getPlaceholderName() + " 参数");
        }
        // 调用实体类属性get方法获取值 并判断这个占位符名称是否是 like 模糊查询  拼接%
        Object paramValue = this.handlingLikeQueries(parameterMapping, invokeMethod.invoke(entity));
        resolveParamList.add(paramValue);
    }
}
